/**
 * 用户
 * 加载视频预约的问诊
 */
var uid, name, avatar, ws, cur_date;
document.addEventListener('plusready',function(){ 
	ws = plus.webview.currentWebview();
	uid = plus.storage.getItem('uid');
	name = plus.storage.getItem('name');
	avatar = plus.storage.getItem('avatar');
	avatar = getAvatar(avatar);
	//获取当前年月日
	var a = new Date();
	var y = a.getFullYear();
	var m = a.getMonth()+1;
	var d = a.getDate();
	if(m < 10)m = '0'+m;
	if(d < 10)d = '0'+d;
	cur_date = y+""+m+""+d;
	showWating();
	console.log(apiRoot+"?m=home&c=chat&a=bespeak&uid="+uid);
	$.ajax({
		type:"get",
		url:apiRoot+"?m=home&c=chat&a=bespeak&uid="+uid,
		dataType :'json',
		success :function(data){
			plus.nativeUI.closeWaiting();
//			console.log(JSON.stringify(data)); 
			var str1 = '';
			var over = '';
			$.each(data.booking, function(k, v) { 
				var timeslot = v.timeslot.replace(/-/g,'');//就诊时间替换成数字格式
				if(parseInt(cur_date) <= parseInt(timeslot)){
					str1 += '<ul class="mui-table-view mtop" aid="'+v.aid+'">'+
						'<li class="mui-table-view-cell mui-media">'+
							'<a href="javascript:;">'+
								'<img class="mui-media-object mui-pull-left" src="'+getAvatar(v.mem.avatar)+'">'+
								'<div class="mui-media-body">'+ 
									v.mem.title+
									'<p class="mui-ellipsis">'+v.addtime+'</p>'+
								'</div>'+
							'</a>'+
							'<span class="span-tx neike">'+v.offices+'</span>'+
						'</li>'+
					'</ul>';
				}else{
					over += v.aid+',';
				}
			});
			$('#item2mobile').html(str1);
			if(over != ''){		//修改状态
				$.ajax({type:"get",url:apiRoot+"/home/chat/overBook",data:{id :over}}); 
			}
			$('.mtop').unbind('tap');
			$('.mtop').on('tap', function(){
				goNewPage('me-bespoke-list-guahao.html', {aid:$(this).attr('aid')})
			})
			var list = '';
			$.each(data.bespeak, function(k,v) {
				list += '<div class="mui-table-view table-view-cell bbs-ico" aid="'+v.aid+'" did="'+v.mem.aid+'" status="'+v.status+'">'+
						'<div class="overflow-h">'+
						'<i><img src="'+getAvatar(v.mem.avatar)+'"/></i>'+
							'<div>'+
								'<li>'+
									'<span>'+v.mem.name+'</span>';
						list += '<span class="mui-h6 inquiry-right color-yellowish">'+(v.status == '9' ? '已完成' : '当前等待：'+v.paidui+'人')+'</span>';
						list += '</li>'+
								'<li class="mui-h6">'+
									'<span class="iconfont type-3">&#xe622;</span>'+
									'<span>预约时间&nbsp;<span class="li'+v.aid+'">'+v.bespeak+'</span></span>'+
								'</li>'+
							'</div>'+
						'</div>'+
					'</div>';
			});
			$('#item1mobile').html(list);
			//超出预约时间可主动发起视频
			$('.bbs-ico').unbind('tap');
			$('.bbs-ico').on('tap', function(){
				var aid = $(this).attr('aid');	//问诊id
				var status = $(this).attr('status');	//问诊状态
				var yue = get_unix_time($('.li'+aid).text());	//预约时间
				var dang = Date.parse(new Date());
				dang = dang.toString().substring(0, dang.toString().length - 3);	//当前时间
				console.log(yue+" "+dang);
				if(yue < dang && status != 9){		//超出预约时间，发起视频
					var doctorid = $(this).attr('did');//医生id
//					console.log(aid+" "+doctorid+" "+name+" "+avatar);return;
					$.ajax({
						type:"get",
						url:apiRoot+"/home/chat/lineUp",
						data:{
							uid :uid,
							did :doctorid,
							wid :aid,
							type :'1'
						},
						success :function(data){
//							mui.toast('前面有 '+data+' 人排队'); 
							if(data > 0){	//有人排队
								toast('医生忙碌，请稍候重试');
							}else{	//发起视频
								aid = aid.toString();
//								mui.toast('发起视频,问诊id:'+aid+" "+doctorid+" "+name+" "+avatar);
								plus.webview.currentWebview().close();
								window.plus.bridge.exec("xiangyu","sendVideo",[doctorid,name,avatar,aid]);
								ws.close();
							}
						},
						error :function(e){
							console.log(JSON.stringify(e));
						}
					});
				}else if(status != 9){
					toast('超出预约时间您可发起视频');
				}
			})
		},
		error :function(e){
			console.log(JSON.stringify(e));
			errortoast();
		}
	});
},false)
//日期转换成时间戳格式
function get_unix_time(dateStr){
    var newstr = dateStr.replace(/-/g,'/'); 
    var date =  new Date(newstr); 
    var time_str = date.getTime().toString();
    return time_str.substr(0, 10);
}